/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 *
 * @author Green
 */
public class GUICard implements Comparable<GUICard> {

    public static final int SPADES = 0;//bích
    public static final int HEARTS = 3;//cơ
    public static final int DIAMONDS = 2;//rô
    public static final int CLUBS = 1;//chuồn

    public static int width;
    public static int height;
    
    public int suit;//Spades,Clubs...
    public int rank;//1,2,3,..,10,11,12,13.
    public boolean isUpSide;//true = ngửa
    public Point position;
    public int owner;

    private boolean isVisible = true;

    private BufferedImage texture;
    private BufferedImage backSide;


    public void setVisible(boolean b) {
        this.isVisible = b;
    }

    public boolean getSide() {
        return this.isUpSide;
    }

    public void setSide(boolean up) {
        this.isUpSide = up;
    }

    public GUICard(int suit, int rank, BufferedImage texture, BufferedImage backside) {
        this.suit = suit;
        this.rank = rank;
        this.isUpSide = false;//ko ngửa
        this.position = new Point(0, 0);
        this.texture = texture;
        this.backSide = backside;
    }

    public void draw(Graphics g) {
        if(!this.isVisible)
            return;
        
        if(isUpSide)
            g.drawImage(texture, position.x, position.y, null);
        else
            g.drawImage(backSide, position.x, position.y, null);
    }

    public int compareTo(GUICard o) {
        if (this.suit > o.suit) {
            return 1;
        } else if (this.suit < o.suit) {
            return -1;
        }

        if(this.rank == 0)//ace
            return 1;

        if(o.rank == 0)//ace
            return -1;

        if (this.rank > o.rank) {
            return 1;
        }
        
        if(this.rank < o.rank) {
            return -1;
        }

        return 0;
    }

    public boolean isSameSuit(GUICard another){
        return this.suit == another.suit;
    }

    public boolean isQueenOfSpade() {
        return this.suit == GUICard.SPADES && this.rank == 12;
    }

    public boolean isTwoOfClub() {
        return this.suit == GUICard.CLUBS && this.rank == 0;
    }

    public boolean hit(Point mousePosition) {
        if(this.position.x < mousePosition.x && this.position.x + GUICard.width > mousePosition.x)
        {
            if(this.position.y < mousePosition.y && this.position.y + GUICard.height > mousePosition.y)
            {
                return true;
            }
        }

        return false;
    }

    public int toCardID() {
        int id = this.suit * 13 + this.rank;
        return id;
    }

    public GUICard clone() {
        GUICard card = new GUICard(suit, rank, texture, backSide);
        return card;
    }
}
